import com.alibaba.fastjson.JSON;
import message.entity.MsgConfig;
import message.handler.IMsgHandler;
import message.handler.JsonMsghandler;
import message.service.MsgService;
import org.dom4j.DocumentException;
import org.junit.Test;
import message.utils.XmlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Jsontest {

    @Test
    public void jsonTest1() throws Exception {
        String filePath = "C:\\Users\\sunxj\\Desktop\\msg\\json\\pack.xml";
        Map source = new HashMap();

        Map head = new HashMap();
        head.put("seqNo", "12344444");
        source.put("head", head);

        Map head1 = new HashMap();
        head1.put("branchNo", "3333333333333333");
        source.put("head1", head1);

        List UserList = new ArrayList();
        Map user1 = new HashMap();
        user1.put("Addr", "上海");
        user1.put("Age", "12");

        List infolist1 = new ArrayList();
        Map info1 = new HashMap();
        info1.put("date", "20211111");
        info1.put("num", "12");
        Map info2 = new HashMap();
        info2.put("date", "20211144");
        info2.put("num", "12");
        infolist1.add(info1);
        infolist1.add(info2);
        user1.put("info", infolist1);

        Map user2 = new HashMap();
        user2.put("Addr", "北京");
        user2.put("Age", "1233");

        List infolist2 = new ArrayList();
        Map info3 = new HashMap();
        info3.put("date", "20211111");
        info3.put("num", "12");
        Map info4 = new HashMap();
        info4.put("date", "20211144");
        info4.put("num", "12");
        infolist2.add(info3);
        infolist2.add(info4);
        user2.put("info", infolist2);

        UserList.add(user1);
        UserList.add(user2);
        Map data1 = new HashMap();
        data1.put("UserList", UserList);


        source.put("data", data1);
        System.out.println(source);
        MsgService msgService = new MsgService();
        MsgConfig msgConfig = msgService.getMsgConfig(filePath);
        byte[] pack = msgService.pack(msgConfig, source);
        System.out.println(new String(pack,"GB2312"));
    }

    @Test
    public  void jsonUnPack () throws Exception {
        String json = "{\"sysHead\":{\"seqNo\":\"12344444\",\"branchNo\":\"3333333333333333\"},\"userList\":[{\"userInfo\":[{\"DATE\":\"20211111\",\"NUM\":\"12\"},{\"DATE\":\"20211144\",\"NUM\":\"12\"}],\"addr\":\"上海\",\"age\":\"12\"},{\"userInfo\":[{\"DATE\":\"20211111\",\"NUM\":\"12\"},{\"DATE\":\"20211144\",\"NUM\":\"12\"}],\"addr\":\"北京\",\"age\":\"1233\"}]}\n";
        String filePath = "C:\\Users\\sunxj\\Desktop\\msg\\json\\unPack.xml";
        MsgService msgService = new MsgService();
        MsgConfig msgConfig = msgService.getMsgConfig(filePath);
        Map<String, Object> result = msgService.unPack(msgConfig, json.getBytes("GB2312"));
        String s = JSON.toJSONString(result);
        System.out.println(s);
    }
}
